package o2o.web.actions.shop;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.cache.agent.CityAgentMemory;
import o2o.cache.shop.ShopTipMemory;
import o2o.cache.shop.ShopTipRelationMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.config.dao.UserDaoSql;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.agent.CityAgent;
import o2o.dao.PO.shop.ShopTip;
import o2o.dao.PO.shop.ShopTipRelation;
import o2o.dao.PO.user.UserInfo;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.List;

//店铺标签管理
@Controller
public class ShopTipAction {

    //标签列表
    @RequestMapping(method = RequestMethod.POST, value = "/shop/tip/list")
    @ResponseBody
    public String shop_tip_list(String token, Long agentId) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (ValidateUtil.isNotID(agentId)) {
            return AjaxResult.errorResult("商圈不能为空");
        }

        List<ShopTip> tipList = ShopTipMan.tipListOfAgentId(agentId);
        if (ValidateUtil.isAbsList(tipList)) {
            return AjaxResult.successResult(tipList.size(), tipList);
        }
        return AjaxResult.successResult(0, null);
    }


    //添加店铺标签
    @RequestMapping(method = RequestMethod.POST, value = "/shop/tip/add")
    @ResponseBody
    public String shop_tip_add(String token, Long agentId, ShopTip info) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (ValidateUtil.isNotID(agentId)) {
            return AjaxResult.errorResult("商圈不能为空");
        }

        CityAgent cityAgent = CityAgentMemory.getInstance().getById(agentId);
        if (null == cityAgent) {
            return AjaxResult.errorResult("所选商圈不存在");
        }

        long time = DateUtils.getCurrTime();
        info.setCreateTime(time);
        info.setCreateUser(user.getUsername());
        info.setDelFlag(0);

        try {
            UserDaoSql.getInstance().insertObj(PojoTables.SHOP_TIP, info);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("添加失败，请稍后再试");
        }

        //再缓存中新增
        Long tipId = ShopTipMemory.getInstance().add(time);
        // 数据同步
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_TIP_ADD).setLong01(time);
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }
        if (ValidateUtil.isID(tipId)) {
            //添加商圈对应的标签关联关系
            ShopTipRelation shopTipRelation = new ShopTipRelation();
            shopTipRelation.setAgentId(agentId);
            shopTipRelation.setShopTipId(tipId);

            shopTipRelation.setCreateTime(time);
            shopTipRelation.setCreateUser(user.getUsername());
            shopTipRelation.setDelFlag(0);
            shopTipRelation.setOrderNo(0);
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.SHOP_TIP_RELATION, shopTipRelation);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            //店铺关联行业添加缓存
            ShopTipRelationMemory.getInstance().load();
            // 数据同步
            try {
                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_TIP_RELATION_RELOAD);
                JmsSender.setTopicSender(notify);
            } catch (Exception e) {
            }

        }


        return AjaxResult.successResult();
    }


    //编辑店铺标签
    @RequestMapping(method = RequestMethod.POST, value = "/shop/tip/modify")
    @ResponseBody
    public String shop_tip_modify(String token, ShopTip info) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (ValidateUtil.isNotID(info.getId())) {
            return AjaxResult.errorResult("请选择要编辑的");
        }
        ShopTip tip = ShopTipMemory.getInstance().getById(info.getId());
        if (null == tip) {
            return AjaxResult.errorResult("您选择的不存在，请查看");
        }

        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.SHOP_TIP).append(" set code='").append(info.getCode())
                .append("',name='").append(info.getName()).append("',descs='").append(info.getDescs()).append("' ")
                .append(" where delFlag = 0 and id = ").append(info.getId());
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }

        //修改缓存
        ShopTipMemory.getInstance().load();
        // 数据同步
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_TIP_RELOAD);
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        return AjaxResult.successResult();
    }


    //删除店铺标签
    @RequestMapping(method = RequestMethod.POST, value = "/shop/tip/delete")
    @ResponseBody
    public String shop_tip_delete(String token, Long tipId) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (ValidateUtil.isNotID(tipId)) {
            return AjaxResult.errorResult("请先选择标签");
        }

        //数据库中删除
        StringBuffer sb = new StringBuffer();
        sb.append("update ").append(PojoTables.SHOP_TIP).append(" set delFlag = 1,updateUser='").append(user.getUsername()).append("' where id=").append(tipId);
        StringBuffer sbRe = new StringBuffer();
        sbRe.append("update ").append(PojoTables.SHOP_TIP_RELATION).append(" set delFlag = 1,updateUser='").append(user.getUsername()).append("' where shopTipId=").append(tipId);

        try {
            UserDaoSql.getInstance().executeUpdateBySql(sb.toString());
            UserDaoSql.getInstance().executeUpdateBySql(sbRe.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        ShopTipMemory.getInstance().load();
        ShopTipRelationMemory.getInstance().load();
        // 数据同步
        try {
            JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_TIP_AND_RELATION_RELOAD);
            JmsSender.setTopicSender(notify);
        } catch (Exception e) {
        }

        return AjaxResult.successResult();
    }


}

